<table border="0" cellpadding="0" cellspacing="4" width="100%">
	<tr>
		<td>
			<table border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td width="14"></td>
					<td width="120">
						<a href="index.php?admin=noticias&axn=insert_news">Alta de Noticia</a>
					</td>
					<!--td width="120">
						<a href="index.php?admin=noticias&home=Y">Noticia de home</a>
					</td-->
				</tr>
			</table>
		</td>
	</tr>
	<tr><td height="5"></td></tr>
	<tr>
		<td>
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<?php
				if (isset($_REQUEST['tag_id']) && $_REQUEST['tag_id']>0) {
					$newsCountString = "select sum(case when n.status='A' then 1 else 0 end) activos, sum(case when n.status='I' then 1 else 0 end) inactivos from tags t, tags_noticias tn, noticias n where t.tag_id=tn.tag_id and tn.noticia_id=n.noticia_id and t.status='A' and tn.tag_id=".$_REQUEST['tag_id'];
				} else {
					$newsCountString = "select sum(case when status='A' then 1 else 0 end) activos, sum(case when status='I' then 1 else 0 end) inactivos from noticias";
				}
				$newsCountQuery = dbSelect($newsCountString) or die("Error en ".$newsCountString." ".mysql_error());
				if ($newsCountResult = mysql_fetch_array($newsCountQuery)) {
					$activeNews = $newsCountResult['activos'];
					$inactiveNews = $newsCountResult['inactivos'];
				}
				if($activeNews>0 || $inactiveNews>0) {
					?>
					<tr>
						<td>
							<table border='0' cellpadding='0' cellspacing='0' width='100%'>
								<tr>
								<?php
								echo "<td align='left'>Se encontraron ".$activeNews." noticias activas</td>";
								$tagNewsCountString = "select 1 from tags t, tags_noticias tn where t.tag_id=tn.tag_id and t.status='A'";
								$tagNewsCountQuery = dbSelect($tagNewsCountString) or die("Error en ".$tagNewsCountString." ".mysql_error());
								if($tagNewsCountResult = mysql_fetch_array($tagNewsCountQuery)) {
									echo "<td align='right'>Filtrar por tag: <select class='input' name='tags' id='tags' onchange='javascript:window.location.href=this.value;'><option value='index.php?admin=noticias'>Ver Todos</option>";
									$tagSelectString = "select distinct t.tag_id,t.name from tags_noticias tn, tags t where tn.tag_id=t.tag_id and t.status='A' order by t.name";
									$tagSelectQuery = dbSelect($tagSelectString) or die("Error en select ".$tagSelectString." ".mysql_error());
									while($tagSelectResult = mysql_fetch_array($tagSelectQuery)) {
										if($tagSelectResult['tag_id'] == $_REQUEST['tag_id']) {
											echo "<option value='index.php?admin=noticias&tag_id=".$tagSelectResult['tag_id']."' selected>".$tagSelectResult['name']."</option>";
										} else {
											echo "<option value='index.php?admin=noticias&tag_id=".$tagSelectResult['tag_id']."'>".$tagSelectResult['name']."</option>";
										}
									}
									echo "</select></form></td>";
								}
								?>
								</tr>
							</table>
						</td>
					</tr>
					<tr><td height="5"></td></tr>
					<tr><td>
					<table border='1' cellpadding='1' cellspacing='0' width='100%'>
					<tr bgcolor="#CCCCCC">
						<td align="center">
							<table border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td><b>FECHA</b></td>
									<?php if ($activeNews>1) {?>
									<td width="5"></td>
									<td>
										<table border="0" cellpadding="0" cellspacing="0">
											<?php if(!isset($_REQUEST['date_asc'])) { ?>
											<tr><td><a href="index.php?admin=noticias&date_asc=Y&tag_id=<?php echo $_REQUEST['tag_id']; ?>"><img src="../img/s_asc.png" border="0"></a></td></tr>
											<?php } if(!isset($_REQUEST['date_desc'])) { ?>
											<tr><td><a href="index.php?admin=noticias&date_desc=Y&tag_id=<?php echo $_REQUEST['tag_id']; ?>"><img src="../img/s_desc.png" border="0"></a></td></tr>
											<?php } ?>
										</table>
									</td>
									<? } ?>
								</tr>
							</table>
						</td>
						<td align="center">
							<table border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td><b>TITULO</b></td>
									<?php if ($activeNews>1) {?>
									<td width="5"></td>
									<td>
										<table border="0" cellpadding="0" cellspacing="0">
											<?php if(!isset($_REQUEST['title_asc'])) { ?>
											<tr><td><a href="index.php?admin=noticias&title_asc=Y&tag_id=<?php echo $_REQUEST['tag_id']; ?>"><img src="../img/s_asc.png" border="0"></a></td></tr>
											<?php } if(!isset($_REQUEST['title_desc'])) { ?>
											<tr><td><a href="index.php?admin=noticias&title_desc=Y&tag_id=<?php echo $_REQUEST['tag_id']; ?>"><img src="../img/s_desc.png" border="0"></a></td></tr>
											<?php } ?>
										</table>
									</td>
									<? } ?>
								</tr>
							</table>
						</td>
						<td align="center"><b>H</b></td>
						<td align="center"><b>ACCIONES</b></td>
					</tr>
					<?php

					$homeNewString = "select noticia_id from home_config where type='H' and status='A'";
					$homeNewQuery = dbSelect($homeNewString) or die("Error en ".$homeNewString." ".mysql_error());
					$homeNewResult = mysql_fetch_array($homeNewQuery);

					if (isset($_REQUEST['tag_id']) && $_REQUEST['tag_id']>0) {
						$newsShowString = "select n.noticia_id,n.title,n.status,n.insert_dt from noticias n, tags_noticias tn where n.noticia_id=tn.noticia_id and tag_id=".$_REQUEST['tag_id'];
					} else {
						$newsShowString = "select n.noticia_id,n.title,n.status,n.insert_dt from noticias n";
					}
					if(isset($_REQUEST['date_asc']) && $_REQUEST['date_asc']=='Y') {
						$newsShowString = $newsShowString." order by n.insert_dt desc";
					} else if(isset($_REQUEST['date_desc']) && $_REQUEST['date_desc']=='Y') {
						$newsShowString = $newsShowString." order by n.insert_dt";
					} else if(isset($_REQUEST['title_asc']) && $_REQUEST['title_asc']=='Y') {
						$newsShowString = $newsShowString." order by n.title desc";
					} else if(isset($_REQUEST['title_desc']) && $_REQUEST['title_desc']=='Y') {
						$newsShowString = $newsShowString." order by n.title";
					} else {
						$newsShowString = $newsShowString." order by status,insert_dt desc";
					}
					$newsShowQuery = dbSelect($newsShowString) or die("Error en ".$newsShowString." ".mysql_error());
					//Contador de noticias
					while($newsShowResult = mysql_fetch_array($newsShowQuery)) {
						echo "<tr>";
						echo "<td >".date("d/m/Y",strtotime($newsShowResult['insert_dt']))."</td>";
						//echo "<br>".getNaturalUrl($newsShowResult['noticia_id'],$newsShowResult['title']);
						echo "<td width='100%'><a href='../noticias/".getNaturalUrl($newsShowResult['noticia_id'],$newsShowResult['title'])."' target='_blank'>".$newsShowResult['title']."</a></td>";
						if($homeNewResult['noticia_id']!=$newsShowResult['noticia_id'] && $newsShowResult['status']=='A') {
							echo "<td><a href='noticias/operations.php?axn=set_home&noticia_id=".$newsShowResult['noticia_id']."'><img src='../img/b_home.png' border='0'></a></td>";
						} else {
							echo "<td></td>";
						}
						echo "<td align='center'><table border='0' cellspacing='0' cellpadding='0' align='center'><tr>";
						echo "<td><a href='index.php?admin=noticias&axn=update_news&noticia_id=".$newsShowResult['noticia_id']."'><img src='../img/b_edit.png' border='0' title='Modificar noticia'></a></td>";
						echo "<td width='5'></td>";
						if($newsShowResult['status']=='A') {
							echo "<td><a href='noticias/operations.php?axn=inactivate_news&noticia_id=".$newsShowResult['noticia_id']."'><img src='../img/b_drop.png' border='0' title='Inactivar noticia'></a></td>";
						} else {
							echo "<td><a href='noticias/operations.php?axn=activate_news&noticia_id=".$newsShowResult['noticia_id']."'><img src='../img/s_okay.png' border='0' title='Activar noticia'></a></td>";
						}
						echo "</tr></table></td></tr>";
					}
					echo "</table></td></tr>";
				} else {
					echo "<tr><td>No hay noticias cargadas</td></tr>";
				}
				?>
			</table>
		</td>
	</tr>
</table>